/**
 * 训练相关API接口
 */

import { request } from '@/utils/request';

// 类型定义
export interface Training {
  id?: number;
  createTime?: string;
  updateTime?: string;
  agentId: number;
  name: string;
  description: string;
  originalPrompt: string;
  optimizedPrompt?: string;
  status?: string;
  maxIterations: number;
  currentIteration?: number;
}

export interface TrainingExample {
  id?: number;
  createTime?: string;
  updateTime?: string;
  trainingId: number;
  input: string;
  expectedOutput: string;
}

export interface TrainingSession {
  id?: number;
  createTime?: string;
  updateTime?: string;
  trainingId: number;
  iteration: number;
  startTime?: string;
  endTime?: string;
  prompt: string;
  accuracy?: number;
  responseTime?: number;
  promptEfficiency?: number;
  status?: string;
}

export interface TrainingLog {
  id?: number;
  createTime?: string;
  updateTime?: string;
  sessionId: number;
  exampleId: number;
  input: string;
  expectedOutput: string;
  actualOutput: string;
  similarity?: number;
}

// 获取训练列表
export async function getAllTrainings(): Promise<Training[]> {
  return request.get('/api/trainings/list');
}

// 获取单个训练
export async function getTrainingById(id: number): Promise<Training> {
  return request.get('/api/trainings/get', { params: { id } });
}

// 获取智能体相关的训练列表
export async function getTrainingsByAgentId(agentId: number): Promise<Training[]> {
  return request.get('/api/trainings/agent', { params: { agentId } });
}

// 创建训练
export async function createTraining(data: Training): Promise<Training> {
  return request.post('/api/trainings/create', data);
}

// 更新训练
export async function updateTraining(id: number, data: Training): Promise<Training> {
  return request.put(`/api/trainings/update?id=${id}`, data);
}

// 启动训练
export async function startTraining(id: number): Promise<Training> {
  return request.post('/api/trainings/start', null, { params: { id } });
}

// 应用训练结果
export async function applyTrainingResult(id: number): Promise<any> {
  return request.post('/api/trainings/apply', null, { params: { id } });
}

// 删除训练
export async function deleteTraining(id: number): Promise<void> {
  return request.delete('/api/trainings/delete', { params: { id } });
}

// 获取训练示例列表
export async function getExamplesByTrainingId(trainingId: number): Promise<TrainingExample[]> {
  return request.get('/api/training/examples/training', { params: { trainingId } });
}

// 获取单个训练示例
export async function getExampleById(id: number): Promise<TrainingExample> {
  return request.get('/api/training/examples/get', { params: { id } });
}

// 创建训练示例
export async function createExample(data: TrainingExample): Promise<TrainingExample> {
  return request.post('/api/training/examples/create', data);
}

// 批量创建训练示例
export async function createExampleBatch(data: TrainingExample[]): Promise<TrainingExample[]> {
  return request.post('/api/training/examples/batch', data);
}

// 更新训练示例
export async function updateExample(id: number, data: TrainingExample): Promise<TrainingExample> {
  return request.put(`/api/training/examples/update?id=${id}`, data);
}

// 删除训练示例
export async function deleteExample(id: number): Promise<void> {
  return request.delete('/api/training/examples/delete', { params: { id } });
}

// 获取训练会话列表
export async function getSessionsByTrainingId(trainingId: number): Promise<TrainingSession[]> {
  return request.get('/api/training/sessions/training', { params: { trainingId } });
}

// 获取单个训练会话
export async function getSessionById(id: number): Promise<TrainingSession> {
  return request.get('/api/training/sessions/get', { params: { id } });
}

// 获取训练日志列表
export async function getLogsBySessionId(sessionId: number): Promise<TrainingLog[]> {
  return request.get('/api/training/logs/session', { params: { sessionId } });
}

// 获取单个训练日志
export async function getLogById(id: number): Promise<TrainingLog> {
  return request.get('/api/training/logs/get', { params: { id } });
} 